package org.example.codetrail.controller;

import jakarta.validation.constraints.Pattern;
import org.example.codetrail.pojo.Gitee.GiteeRepo;
import org.example.codetrail.pojo.Result;
import org.example.codetrail.pojo.User;
import org.example.codetrail.service.UserService;
import org.example.codetrail.utils.GiteeRepoFetcher;
import org.example.codetrail.utils.JwtUtil;
import org.example.codetrail.utils.Md5Util;
import org.example.codetrail.utils.ThreadLocalUtil;
import org.example.codetrail.utils.GiteeRepoFetcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/add")
    public Result add(String username, String password) throws IOException {
        User u = userService.findByUserName(username);
        if (u == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @PostMapping("/login")
    public Result login(String username, String password) {
        User loginUser = userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名错误");
        }
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //存到redis中
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set("id",loginUser.getId().toString() , 12, TimeUnit.HOURS);
            operations.set("username",loginUser.getUsername(),12,TimeUnit.HOURS);
            return Result.success("登录成功");
        }
        return Result.error("密码错误");
    }


    @GetMapping("/userInfo")
    public Result<User> getInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody User user){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        user.setId(id);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/getRepos")
    public Result<List<GiteeRepo>> getRepo(String platformName) throws IOException {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        if (user.getAccessToken()==null)return Result.error("请先绑定"+platformName+"平台账号");


        String accessToken = user.getAccessToken().get(platformName);
        if (accessToken==null)return Result.error("请先绑定"+platformName+"平台账号");
        else if (platformName.equals("gitee")){
            //把accessToken放在redis中
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set("accessToken",accessToken , 12, TimeUnit.HOURS);
            operations.set("platformName",platformName , 12, TimeUnit.HOURS);
            // 调用方法获取数组
            GiteeRepo[] reposArray = GiteeRepoFetcher.fetchAndPrintRepoNames(accessToken);
            // 将数组转换为 List
            List<GiteeRepo> reposList = Arrays.asList(reposArray);
            // 返回转换后的 List
            return Result.success(reposList);
        }
        return Result.error("暂不支持该平台");
    }
}
